import ajax from '../libs/ajax.js';
import tools from '../libs/tools.js';

let gjsj;
let sy = document.getElementById('sy');
sy.addEventListener('click', () => {
  location.href = './twtwz.html';
});

//#region 内容
function geturl() {
  let params = location.search.length > 0 ? location.search.substring(1) : '',
    args = {},
    items = params.length ? params.split('&') : [],
    item = '',
    key = '',
    value = '',
    len = items.length;

  for (let i = 0; i < len; i++) {
    item = items[i].split('=');
    key = decodeURIComponent(items[0]);
    value = decodeURIComponent(item[1]);
    gjsj = value;
    if (key.length) {
      args[key] = value;
    }
  }

  return args;
}
geturl();
console.log(gjsj);

let pingl = document.getElementById('pingl');
let mc = document.getElementById('mc');
let imgLogo11 = document.getElementById('imgLogo11');
let imgLogo12 = document.getElementById('imgLogo12');

//#endregion

//#region 举报
let jbDialog = document.getElementById('jbDialog');
let txtatitle = document.getElementById('txtatitle');
let btnjb = document.getElementById('btnjb');

btnjb.addEventListener('click', () => {
  let info = {
    info: txtatitle.value,
    umid: gjsj
  };
  ajax.send('/message/examine', info, (data) => {
    showToast(data.message);
    if (data.success) {
      txtatitle.value = '';
      txtatitle.focus();
    }
  });
});

jbDialog.addEventListener('click', () => {
  prlb();
});

//#endregion

//#region 评论列表展示
let info = {
  orderBy: 1,
  umid: gjsj
};
let pe = {
  pageNumber: 1,
  pageSize: 7
};

let sz = [];
//用户基本信息
let sz1 = [];
function prlb() {
  ajax.send('/message/queryDetail', tools.concatJson(info, pe), (data) => {
    if (data.success) {
      sz = data.list;
      sz1 = data.info;
      console.log(sz);
      pe = data.page;
      lbzs();
      yhzs();
      fy();
    }
  });
}
console.log('nnnnn',sz1)
function yhzs() {
  console.log('nnnnn',sz1)
  pingl.innerHTML = sz1.info;
  mc.innerHTML = sz1.user.nickname;
  imgLogo11.src = `${sz1.userInfo.img}`;
  imgLogo12.src = `${sz1.userInfo.img}`;
  //点赞
  let dz0 = document.getElementById('dz0');
  dz0.innerHTML = sz1.praiseCount + '点赞';

  dz0.addEventListener('click', () => {
    let css = {
      umid: sz1.umid
    };
    ajax.send('/message/support', css, (data) => {
      if (data.message) {
        showToast(data.message);
        prlb();
      }
    });
  });
  //点击量
  let dj0 = document.getElementById('dj0');
  dj0.innerHTML = sz1.hits + '点击量';

  //#region 添加好友
  let cs = {
    username: sz1.user.username,
  };
  console.log('ccccc',cs)
let btnadd = document.getElementById('btnadd');
btnadd.addEventListener('click', () => {
  ajax.send('/user/auth/friend', cs, (data) => {
    console.log('cssss',cs)
    showToast(data.message);
    if (data.success) {
      prlb();
    }
  });
});

//#endregion


//#region 关注
let btngz = document.getElementById('btngz');
btngz.addEventListener('click', () => {
  console.log('css',cs.username)
  ajax.send('/message/followUser', cs, (data) => {
    showToast(data.message);
    if (data.success) {
      prlb();
    }
  });
});
}

function lbzs() {
  let datalist = document.getElementById('datalist');
  datalist.innerHTML = '';
  for (let i = 0; i < sz.length; i++) {
    const x = sz[i];
    console.log('x', x);
    //div0 容器
    let div0 = document.createElement('div');
    div0.classList.add('item');
    //img1 图片
    let img1 = document.createElement('img');
    img1.classList.add('avator');
    if (x.userInfo.img) {
      img1.setAttribute('src', x.userInfo.img);
    } else {
      img1.setAttribute('scr', '../img/e.jpg');
    }
    div0.append(img1);
    datalist.append(div0);

    //div2 内容==========
    let div2 = document.createElement('div');
    div2.classList.add('info');
    //div3 账号
    let div3 = document.createElement('div');
    div3.classList.add('email');
    div3.append(x.user.nickname);
    div2.append(div3);
    div0.append(div2);
    datalist.append(div0);

    //div4 时间
    let div4 = document.createElement('div');
    div4.classList.add('time');
    div4.append(tools.FormData(x.lastupdate));
    div2.append(div4);
    div0.append(div2);
    datalist.append(div0);

    //评论信息
    let div5 = document.createElement('div');
    div5.classList.add('text', 'word3');
    div5.append(x.info);
    div2.append(div5);
    div0.append(div2);
    datalist.append(div0);

    //点赞，举报包裹div6
    let div6 = document.createElement('div');
    div6.classList.add('do');
    div2.append(div6);
    div0.append(div2);
    datalist.append(div0);

    //举报按钮div7
    let div7 = document.createElement('div');
    div7.append('举报');
    div7.classList.add('good', 'btn');
    div6.append(div7);
    div2.append(div6);
    div0.append(div2);
    datalist.append(div0);
    let dialog = document.querySelector('dialog');
    div7.addEventListener('click', () => {
      dialog.showModal();
    });
    let txttitle = document.getElementById('txttitle');
    let btnjbm = document.getElementById('btnjbm');
    btnjbm.addEventListener('click', () => {
      let info = {
        info: txttitle.value,
        umrid: x.umrid
      };
      ajax.send('/message/examineReply', info, (data) => {
        showToast(data.message);
        if (data.success) {
          txttitle.value = '';
          prlb();
        }
      });
    });
    //   let dj1 = document.getElementById('dj1');
    //   dj1.innerHTML = '举报';
    //   let dialog = document.querySelector('dialog');
    //   dj1.addEventListener('click', () => {
    //     dialog.showModal();
    //   });
    //   let txttitle=document.getElementById('txttitle');
    //   let btnjbm=document.getElementById('btnjbm');
    //   btnjbm.addEventListener('click', () => {
    //     let info = {
    //       info: txttitle.value,
    //       umrid: x.umrid,
    //     };
    //     ajax.send('/message/examineReply', info, (data) => {
    //       showToast(data.message);
    //       if (data.success) {
    //         txttitle.value = '';
    //         txttitle.focus();
    //       }
    //     });
    //   });

    //点赞按钮
    let div8 = document.createElement('div');
    div8.append(x.praiseCount + '点赞');
    div8.classList.add('look', 'btn');
    div6.append(div8);
    div2.append(div6);
    div0.append(div2);
    datalist.append(div0);
    div8.addEventListener('click', () => {
      let css = {
        umrid: x.umrid
      };
      ajax.send('/message/supportReply', css, (data) => {
        showToast(data.message);
        if (data.success) {
          prlb();
        }
      });
    });
    //   //点赞
    //   let dianzhan1 = document.getElementById('dianzhan1');
    //   dianzhan1.innerHTML = x.praiseCount + '点赞';
    //   dianzhan1.addEventListener('click', () => {
    //     let css = {
    //       umrid: x.umrid
    //     };
    //     ajax.send('/message/supportReply', css, (data) => {
    //       if (data.message) {
    //         showToast(data.message);
    //       }
    //     });
    //   });
    // }
  }
}

//#endregion




//#region 评论列表分页
let pages = document.querySelectorAll('#navpage>span');
function fy() {
  pages[1].innerHTML = `
当前页/总页数/记录数：
${pe.pageNumber}/${pe.pageCount}/${pe.total}
`;
}

pages[0].addEventListener('click', () => {
  pe.pageNumber--;
  if (pe.pageNumber < 1) {
    pe.pageNumber = 1;
    return;
  }
  console.log('000000000000000000');
  prlb();
});

pages[2].addEventListener('click', () => {
  pe.pageNumber++;
  if (pe.pageNumber > pe.pageCount) {
    pe.pageNumber = pe.pageCount;
    return;
  }
  console.log('22222222222222222222');
  console.log('下一页');
  prlb();
});

//#endregion

//#region 发布留言信息
let nr = document.getElementById('nr');
let ok = document.getElementById('ok');
ok.addEventListener('click', () => {
  let cs = {
    info: nr.value,
    umid: gjsj
  };
  ajax.send('/message/addReply', cs, (data) => {
    if (data.success) {
      showToast(data.message);
      nr.value = '';
      prlb();
    }
  });
});

//#endregion

//#region 登录用户信息查询

let tbUser = {};
let tbUserInfo = {};
let userOtherInfo = {};

let spUser = document.getElementById('spUser');
let imgLogo = document.getElementById('imgLogo');

function queryUserInfo() {
  ajax.send('/user/auth/getUserInfo', {}, (data) => {
    if (data.success) {
      tbUser = data.tbUser;
      tbUserInfo = data.tbUserInfo;
      userOtherInfo = data.userOtherInfo;
      showUser();
    } else {
      showToast('用户未登录');
    }
  });
}

function showUser() {
  spUser.innerHTML = `${tbUser.nickname}`;

  txtNickname.value = tbUser.nickname;

  txtInfo.value = tbUserInfo.info;
  txtQq.value = tbUserInfo.qq;
  txtWechat.value = tbUserInfo.wechat;
  // 性别显示
  if ('m' == tbUserInfo.sex) {
    sexm.checked = true;
  } else if ('f' == tbUserInfo.sex) {
    sexf.checked = true;
  } else if ('n' == tbUserInfo.sex) {
    sexn.checked = true;
  }

  // 处理用户头像
  if (tbUserInfo.img != '') {
    imgLogo.setAttribute('src', tbUserInfo.img);
  }
  let sex = '保密';
  if (tbUserInfo.sex == 'm') {
    sex = '男神';
  } else if (tbUserInfo.sex == 'f') {
    sex = '女神';
  }
  // 显示信息的部分
  divShowInfo.innerHTML = `
    描述：${tbUserInfo.info}<br>
    性别：${sex}<br>
    QQ：${tbUserInfo.qq}<br>
    微信：${tbUserInfo.wechat}<br>
    手机号：${tbUserInfo.phone}<br>
    邮箱：${tbUserInfo.email}<br>
    注册天数：${userOtherInfo.regDays}天<br>
  `;
}

//#endregion

//#region 用户信息修改
// https://service.huhuiyu.top/teach_project_service/user/file/download?fid=2
let userDialog = document.getElementById('userDialog');
let txtInfo = document.getElementById('txtInfo');
let txtNickname = document.getElementById('txtNickname');
let txtQq = document.getElementById('txtQq');
let txtWechat = document.getElementById('txtWechat');
let sexm = document.getElementById('sexm');
let sexf = document.getElementById('sexf');
let sexn = document.getElementById('sexn');
let btnSave = document.getElementById('btnSave');

let divModify = document.getElementById('divModify');
let divShowInfo = document.getElementById('divShowInfo');
let btnChange = document.getElementById('btnChange');

btnChange.addEventListener('click', () => {
  if ('编辑' == btnChange.innerText) {
    btnChange.innerText = '返回';
    divShowInfo.style.display = 'none';
    divModify.style.display = 'block';
    btnSave.classList.remove('disabled');
  } else {
    btnChange.innerText = '编辑';
    divShowInfo.style.display = 'block';
    divModify.style.display = 'none';
    btnSave.classList.add('disabled');
  }
});

btnSave.addEventListener('click', () => {
  let info = {
    info: txtInfo.value,
    nickname: txtNickname.value,
    qq: txtQq.value,
    wechat: txtWechat.value,
    sex: 'n'
  };
  // 处理性别
  if (sexm.checked) {
    info.sex = 'm';
  } else if (sexf.checked) {
    info.sex = 'f';
  }
  // 保存修改
  ajax.send('/user/auth/updateUserInfo', info, (data) => {
    showToast(data.message);
  });
});

// 关闭时要重新查询用户信息
userDialog.addEventListener('hide.bs.modal', () => {
  queryUserInfo();
});

//#endregion

//#region 安全退出
let spExit = document.getElementById('spExit');

spExit.addEventListener('click', () => {
  ajax.send('/user/auth/logout', {}, () => {
    location.href = './twt.html';
  });
});

//#endregion

//#region 点击图片上传
imgLogo.addEventListener('click', () => {
  //浏览文件
  tools.openFile((file) => {
    if (!file) {
      return;
    }
    //上传图像
    ajax.file(file, '用户图像', (data) => {
      if (!data.success) {
        return;
      }

      //如果原始头像也是上传的文件，要删除，避免上传图片越来越多
      if (ajax.isFileurl(tbUserInfo.img)) {
        let fid = ajax.geturlfid(tbUserInfo.img);
        ajax.send('/user/file/delete', { fid: fid }, () => {});
      }

      //获取上传文件url
      let url = ajax.getFileUrl(data.data.fid);
      //修改用户的头像地址信息
      tbUserInfo.img = url;
      let info = JSON.parse(JSON.stringify(tbUserInfo));
      info.nickname = tbUser.nickname;

      ajax.send('/user/auth/updateUserInfo', info, () => {
        queryUserInfo();
      });
    });
  });
});
//#endregion

//#region 邮箱修改
let txtEmail = document.getElementById('txtEmail');
let txtEmailCode = document.getElementById('txtEmailCode');
let btnEmailCode = document.getElementById('btnEmailCode');
let btnEmail = document.getElementById('btnEmail');

// 获取邮箱验证码
btnEmailCode.addEventListener('click', () => {
  let info = {
    email: txtEmail.value
  };
  ajax.send('/tool/sendEmailCode', info, (data) => {
    showToast(data.message);
  });
});

// 修改邮箱
btnEmail.addEventListener('click', () => {
  let info = {
    email: txtEmail.value,
    code: txtEmailCode.value
  };
  ajax.send('/user/auth/updateUserEmail', info, (data) => {
    showToast(data.message);
    if (data.success) {
      queryUserInfo();
    }
  });
});

//#endregion

//#region 手机修改
let txtphone = document.getElementById('txtphone');
let txtcode = document.getElementById('txtcode');
let btncode = document.getElementById('btncode');
let btnbd = document.getElementById('btnbd');
let txtimageCode = document.getElementById('txtimageCode');
let imageCode = document.getElementById('imageCode');

btncode.addEventListener('click', () => {
  ajax.send(
    '/tool/sendValidateCode',
    {
      phone: txtphone.value,
      imageCode: txtimageCode.value
    },
    (data) => {
      showToast(data.message);
    }
  );
});

btnbd.addEventListener('click', () => {
  if (txtcode.value.trim() == '') {
    showToast('验证码不能为空');
    return;
  }

  let cs = {
    code: txtcode.value,
    phone: txtphone.value
  };

  ajax.send('/user/auth/updateUserPhone', cs, (data) => {
    showToast(data.message);
    if (data.success) {
      queryUserInfo();
    }
  });
});
//#endregion
//#region 获取图片验证码
//通过ajax获取图片验证码的function

function loadImgCode() {
  ajax.send('/tool/getImageCode', {}, (info) => {
    imageCode.setAttribute('src', info.message);
  });
}

imageCode.addEventListener('click', loadImgCode);

loadImgCode();

btncode.addEventListener('click', () => {
  //验证图片验证码
  ajax.send(
    '/tool/checkImageCode',
    {
      code: txtimageCode.value
    },
    (info) => {
      alert(info.message);
      //不论正确与否，验证码只能使用1次
      //必须重新更新,验证码不区分大小写
      loadImgCode();
    }
  );
});

//#endregion

//#region 公用的轻提示对话框

let liveToast = document.getElementById('liveToast');
let liveToastMessing = document.querySelector('#liveToast .toast-body');

const toast = new bootstrap.Toast(liveToast);

function showToast(message) {
  liveToastMessing.innerHTML = message;
  toast.show();
}
//#endregion

queryUserInfo();
prlb();
